/**
 * 文件名：****.java
 * 版权：Copyright Lightkit All Rights Reserved.
 * 描述：*****
 */
package com.lightkits.wms.domain.usuallyinterface;

import com.lightkits.common.TransactionValidate;
import com.lightkits.common.db.entity.FndWarehouse;
import com.lightkits.common.db.entity.FndWmArea;
import com.lightkits.framework.common.exception.LightKitsBizException;
import com.lightkits.framework.common.model.IResult;
import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.framework.common.utility.validate.IntegrityValidator;
import com.lightkits.framework.soa.hzero.executor.ServiceExecutor;
import com.lightkits.wms.database.dao.WmsFndWmAreaMapper;
import com.lightkits.wms.database.dao.WmsWarehouseMapper;
import com.lightkits.wms.database.model.FndWarehouseByIdModel;
import com.lightkits.wms.database.model.FndWmAreaByIdModel;
import com.lightkits.wms.dto.usuallyinterface.GetWareAndAreaRequestType;
import com.lightkits.wms.dto.usuallyinterface.GetWareAndAreaResponseType;
import com.lightkits.wms.dto.usuallyinterface.model.WareAndWmDTO;
import com.lightkits.wms.util.DomainUtil;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * GetWareAndAreaExecutor
 *
 * @author jing.wei@hand-china.com
 * Created on 2019/4/19 10:04
 */
@Component
public class GetWareAndAreaExecutor implements ServiceExecutor<GetWareAndAreaRequestType,
        GetWareAndAreaResponseType> {
    private final WmsWarehouseMapper wmsWarehouseMapper;
    private final WmsFndWmAreaMapper fndWmAreaMapper;

    public GetWareAndAreaExecutor(WmsWarehouseMapper wmsWarehouseMapper,
                                  WmsFndWmAreaMapper fndWmAreaMapper) {
        this.wmsWarehouseMapper = wmsWarehouseMapper;
        this.fndWmAreaMapper = fndWmAreaMapper;
    }

    /**
     * 获取仓库货位详情服务
     *
     * @param request 获取请求参数
     * @return GetWareAndAreaResponseType
     **/
    @Override
    public GetWareAndAreaResponseType service(GetWareAndAreaRequestType request) {
        GetWareAndAreaResponseType response = new GetWareAndAreaResponseType();
        response.setSuccessful(true);
        try {
            //参数校验
            IResult result = checkParams(request.getWarehouseId(), request.getWmAreaId());
            if (!result.isSuccessful()) {
                result.fill(response);
                return response;
            }
            //获取仓库详情
            List<FndWarehouseByIdModel> warehouseByIdModels =
                    wmsWarehouseMapper.quryWarehouseById(request.getWarehouseId());
            //获取货位详情
            List<FndWmAreaByIdModel> wmAreaByIdModels =
                    fndWmAreaMapper.queryWmAreaById(request.getWmAreaId());
            WareAndWmDTO wareAndWmDTO = new WareAndWmDTO();
            if (!ObjectUtil.isNullOrEmpty(warehouseByIdModels)
                    && warehouseByIdModels.size() == 1) {
                FndWarehouseByIdModel model = warehouseByIdModels.get(0);
                wareAndWmDTO.setWarehouseId(model.getWarehouseId());
                wareAndWmDTO.setWarehouseName(model.getWarehouseName());
                wareAndWmDTO.setWarehouseCategroy(model.getWarehouseCategroy());
                wareAndWmDTO.setWarehouseLocation(model.getWarehouseLocation());
                wareAndWmDTO.setOnhandFlag(DomainUtil.convertFlag(model.getOnhandFlag()));
                wareAndWmDTO.setNegativeFlag(DomainUtil.convertFlag(model.getNegativeFlag()));
                wareAndWmDTO.setWmAreaFlag(DomainUtil.convertFlag(model.getWmAreaFlag()));
                wareAndWmDTO.setWmUnitFlag(DomainUtil.convertFlag(model.getWmUnitFlag()));
            }
            if (!ObjectUtil.isNullOrEmpty(wmAreaByIdModels)
                    && wmAreaByIdModels.size() == 1) {
                FndWmAreaByIdModel model = wmAreaByIdModels.get(0);
                wareAndWmDTO.setWmAreaId(model.getWmAreaId());
                wareAndWmDTO.setWmAreaName(model.getWmAreaName());
                wareAndWmDTO.setWmAreaCode(model.getWmAreaCode());
                wareAndWmDTO.setWmAreaCategory(model.getWmAreaCategory());
                wareAndWmDTO.setWmAreaLocatin(model.getWmAreaLocatin());
            }
            response.setWareAndWmDTO(wareAndWmDTO);
        } catch (Exception e) {
            response.setSuccessful(false);
            response.setMessage("get Warehouse And WmArea error");
        }
        return response;
    }

    /**
     * 校验仓库货位参数
     *
     * @param warehouseId 仓库id
     * @param wmAreaId    货位ID
     * @return IResult
     **/
    private IResult checkParams(String warehouseId, String wmAreaId) {
        try {
            IntegrityValidator.begin()
                    .validate(warehouseId, "Warehouse Id");
            TransactionValidate check1 = TransactionValidate.beginValidity(FndWarehouse.class);
            check1.checkIdValidity(ObjectUtil.toString(warehouseId), "ID");
            check1 = TransactionValidate.beginValidity(FndWmArea.class);
            check1.checkIdValidity(ObjectUtil.toString(wmAreaId), "ID");
        } catch (LightKitsBizException e) {
            return e.result();
        }
        return IResult.success();
    }


}


